package com.jboa.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.jboa.model.Email;
import com.jboa.model.Employee;
import com.jboa.service.EmailBiz;
import com.jboa.service.EmployeeBiz;
import com.jboa.util.PageBean;
import com.jboa.util.PageMaker;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EmailAction extends ActionSupport {

	private EmailBiz emailBiz;
	private EmployeeBiz employeeBiz;
	private List<Email> emails;
	private List<Employee> employees;
	private Email email;
	private File attachment;
	private int eid;
	private String uploadPath;
	private String attachmentFileName;//文件名称
	private String attachmentContentType;//文件类型
	private InputStream inputStream;	//用于下载文件
	private int page=1;
	private String pageView;
//	阿什顿分割
	
	public String getPageView() {
		return pageView;
	}

	public void setPageView(String pageView) {
		this.pageView = pageView;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getAttachmentContentType() {
		return attachmentContentType;
	}

	public void setAttachmentContentType(String attachmentContentType) {
		this.attachmentContentType = attachmentContentType;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getAttachmentFileName() {
		return attachmentFileName;
	}

	public void setAttachmentFileName(String attachmentFileName) {
		this.attachmentFileName = attachmentFileName;
	}

	public void setUploadPath(String uploadPath) {
		this.uploadPath = uploadPath;
	}
	
	public int getEid() {
		return eid;
	}

	public void setEid(int eid) {
		this.eid = eid;
	}

	public File getAttachment() {
		return attachment;
	}

	public void setAttachment(File attachment) {
		this.attachment = attachment;
	}

	public Email getEmail() {
		return email;
	}

	public void setEmail(Email email) {
		this.email = email;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

	public void setEmailBiz(EmailBiz emailBiz) {
		this.emailBiz = emailBiz;
	}
	
	public EmployeeBiz getEmployeeBiz() {
		return employeeBiz;
	}

	public void setEmployeeBiz(EmployeeBiz employeeBiz) {
		this.employeeBiz = employeeBiz;
	}

	public List<Email> getEmails() {
		return emails;
	}

	public void setEmails(List<Email> emails) {
		this.emails = emails;
	}
	/**
	 * 收件箱显示当前用户的所有邮件（不含已经删除的）
	 * @return
	 */
	public String accept(){
		Employee employee = (Employee)ActionContext.getContext().getSession().get("curruser");
		String username = employee.getName();
		emails =this.emailBiz.acceptEmail(username);
		return "accecpt_success";
	}
	public String sended(){
		//获取当前用户名
		Employee employee = (Employee)ActionContext.getContext().getSession().get("curruser");
		String username = employee.getName();
		//ָ指定当前页
		PageBean pageBean=new PageBean();
		pageBean.setCurtPage(page);	
		//获取总记录数
		String hql="from Email where usernameFrom='"+username+"'";
		int totalSize =this.emailBiz.findTotalCount(hql);	
		System.out.println(totalSize);
		pageBean.setTotalSize(totalSize);
		//根据当前页和总记录数组装页面分页显示字符串
		PageMaker maker = new PageMaker();
		
		pageView=maker.makeList(pageBean, "email1!sended.action?page=");		
		//获取当前页数据
		int maxResults =pageBean.getPageSize();
		int firstResult=(pageBean.getCurtPage()-1)*maxResults;
		emails =this.emailBiz.sendedEmail(username,firstResult,maxResults);
		
		return "sended_success";
	}
	public String deleted(){
		Employee employee = (Employee)ActionContext.getContext().getSession().get("curruser");
		String username = employee.getName();
		emails =this.emailBiz.deletedEmail(username);
		return "deleted_success";
	}
	public String toadd(){
		Employee employee = (Employee)ActionContext.getContext().getSession().get("curruser");
		String username = employee.getName();
		employees =this.employeeBiz.findUsers(username);
		return "toadd_success";
	}
	public String doadd(){
		
		//得到保存上传文件的真实路径
		String realPath =ServletActionContext.getServletContext().getRealPath(uploadPath);
		//如果路径不存在，则创建它
		File dir = new File(realPath);
		if(!dir.exists()){
			dir.mkdir();
		}
		//创建输入和输出流
		InputStream fis=null;
		OutputStream fos=null;
		try {
			fis = new FileInputStream(attachment);
			//System.out.println(this.fileFileName);
			//fos = new FileOutputStream(new File(dir,this.fileFileName));
			fos = new FileOutputStream(new File(dir,attachmentFileName));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		//完成复制操作
		byte buf[] = new byte[4096];
		int len;
		try {
			len = fis.read(buf);
			while(len!=-1){
				fos.write(buf, 0, len);
				len = fis.read(buf);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		//关闭输入流输出流
		try {
			fis.close();
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Employee employee = (Employee)ActionContext.getContext().getSession().get("curruser");
		email.setCreatetime(new Date());
		email.setUsernameFrom(employee.getName());
		email.setState("未读");
		email.setAttachment(this.uploadPath+"/"+attachmentFileName);
		email.setFiletype(attachmentContentType);
		this.emailBiz.addEmail(email);
		return "doadd_success";
	}
	public String search(){
		email =this.emailBiz.searchById(eid);
		return "search_success";
	}
	public String delete(){
		this.emailBiz.deleteById(eid);
		return "delete_success";
	}
	
	public String delete2(){
		this.emailBiz.deleteById2(eid);
		return "delete2_success";
	}
	public String reverse(){
		this.emailBiz.reverse(eid);
		return "reverse_success";
	}
	
	public String download(){
		Email email =this.emailBiz.searchById(eid);
		String filePath = email.getAttachment();
		attachmentFileName=filePath.substring(filePath.lastIndexOf("/")+1);
		try {
			//解决中文乱码问题
			attachmentFileName=new String(attachmentFileName.getBytes("utf-8"),"iso-8859-1");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println(attachmentFileName);
		attachmentContentType=email.getFiletype();
		//得到保存上传文件的真实路径
		String realPath =ServletActionContext.getServletContext().getRealPath(filePath);
			try {
			inputStream =new FileInputStream(new File(realPath));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "download_success";
	}
	
}
